package com.wn.airplane_mode.activity;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.wn.airplane_mode.R;
import com.wn.airplane_mode.service.AirplaneModeOpService;
import com.wn.airplane_mode.tool.AirplaneTool;
/**
 * @author wuning
 * */
public class AirplaneModeOpNewActivity extends TabActivity {
	
	private final static String SETTING1 = "normal setting";
	private final static String SETTING2 = "cycle setting";
	
	private ToggleButton tbtn_now_startup_airplanemode;//现在启动飞行模式开关
	//private ToggleButton tbtn_icon_on_statusbar;//图标常驻状态栏开关
	private ToggleButton tbtn_auto_startup_service;//开机自动启动后台服务开关
	private ToggleButton tbtn_op_service;//现在启动后台服务
	private TextView versionInf = null;
	
	private AirplaneTool airplaneTool = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Log.i("AirplaneModeOpNewActivity", "onCreate..."+this);
		
		setContentView(R.layout.main);
		
		airplaneTool = AirplaneTool.getInstance(this);
		airplaneTool.setAirplaneModeHandler(handler);
		
		TabHost tabHost = getTabHost();
		TabSpec spec = null;
		airplaneTool.tabHost = tabHost;
		
		spec = tabHost.newTabSpec(SETTING1).setIndicator(createTabView(R.string.setting_1)).setContent(R.id.setting1_id);
		tabHost.addTab(spec);
		
		spec = tabHost.newTabSpec(SETTING2).setIndicator(createTabView(R.string.setting_2)).setContent(new Intent(this, AirplaneModeCycleSettings.class));
		tabHost.addTab(spec);
		
		findView();
		setListener();
	}
	

	@Override
	protected void onResume() {
		Log.i("AirplaneModeOpNewActivity", "onResume..."+this);
		initView();
		super.onResume();
	}
	
	@Override
	protected void onDestroy() {
		airplaneTool.reset();
		super.onDestroy();
	}
	
	/**自定义tab样式*/
	private View createTabView(int resId){
		View view = LayoutInflater.from(this).inflate(R.layout.airplane_tab_indicator, null);
        TextView title = (TextView) view.findViewById(R.id.tab_title);
        title.setText(resId);
        return view;

	}
	
	private void findView(){
		tbtn_now_startup_airplanemode = (ToggleButton)findViewById(R.id.toggleButton_startup_airplane);
		//tbtn_icon_on_statusbar = (ToggleButton)findViewById(R.id.toggleButton_icon_on_statusbar);
		tbtn_auto_startup_service = (ToggleButton)findViewById(R.id.toggleButton_autostartup_service);
		tbtn_op_service = (ToggleButton)findViewById(R.id.toggleButton_op_service);
		
		versionInf = (TextView)findViewById(R.id.text_version_inf);
		versionInf.setText(getAppVersionName(this));
	}
	
	/**    
	* 返回当前程序版本名    
	*/     
	public static String getAppVersionName(Context context) {     
	    String versionName = "";     
	    try {     
	        PackageManager pm = context.getPackageManager();     
	        PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);     
	        versionName = pi.versionName;     
	        if (versionName == null || versionName.length() <= 0) {     
	            return "";     
	        }     
	    } catch (Exception e) {     
	        Log.e("VersionInfo", "Exception", e);     
	    }     
	    return versionName;     
	}    

	
	private void initView(){
		tbtn_now_startup_airplanemode.setChecked(airplaneTool.isAirPlaneModeOn());
		//tbtn_icon_on_statusbar.setChecked(airplaneTool.isIconOnstatusbar());
		tbtn_auto_startup_service.setChecked(airplaneTool.isAutoStartupService());
		tbtn_op_service.setChecked(airplaneTool.isServiceRunning());
	
	}
	
	private void setListener(){
		tbtn_now_startup_airplanemode.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(airplaneTool.setAirPlaneModeOn(isChecked)){
					//操作成功
					if(isChecked){
						new AirplaneModeToast(getApplicationContext(), R.string.startup_airplanemode_ok_showtext).show();
					}else{
						new AirplaneModeToast(getApplicationContext(), R.string.shutdown_airplanemode_ok_showtext).show();
					}
					
				}else{
					//操作失败
					if(isChecked){
						new AirplaneModeToast(getApplicationContext(), R.string.startup_airplanemode_er_showtext).show();
					}else{
						new AirplaneModeToast(getApplicationContext(), R.string.shutdown_airplanemode_er_showtext).show();
					}
				}
				
				
			}
		});
//		tbtn_icon_on_statusbar.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//					
//			@Override
//			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//				airplaneTool.setIconOnstatusbar(isChecked);	
//						
//			}
//		});
		tbtn_auto_startup_service.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				airplaneTool.setAutoStartupService(isChecked);
				
			}
		});
		
		tbtn_op_service.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				
				Intent service = new Intent(AirplaneModeOpNewActivity.this,AirplaneModeOpService.class);
				
				if(isChecked && !airplaneTool.isServiceRunning()){
					if(startService(service)!=null){
						
						//airplaneTool.setServiceRunning(true);
						new AirplaneModeToast(getApplicationContext(), R.string.startup_service_showtext).show();
					}else{
						new AirplaneModeToast(getApplicationContext(), R.string.startup_service_error).show();
					}
				}else if(!isChecked && airplaneTool.isServiceRunning()){
					stopService(service);
					//airplaneTool.setServiceRunning(false);
					new AirplaneModeToast(getApplicationContext(), R.string.shutdown_service_showtext).show();
				}
			
				
			}
		});
	}
	AirplaneModeHandler handler = new AirplaneModeHandler();
	public class AirplaneModeHandler extends Handler{
		public final static byte WHAT_CHANGE_BTN_STARTUP = 1;
		@Override
		public void handleMessage(Message msg) {
			
			switch(msg.what){
			case WHAT_CHANGE_BTN_STARTUP:
				tbtn_now_startup_airplanemode.setChecked(airplaneTool.isAirPlaneModeOn());
				break;
			}
		}
	}
	
	
	private byte KEYCODE_BACK_NUM = 0;
	private long now = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	
    	if(KEYCODE_BACK_NUM > 0 && System.currentTimeMillis()-2000 <= now){
    		KEYCODE_BACK_NUM = 0;
    		now = 0;
    		finish();
    		return true;
    	}else{
    		KEYCODE_BACK_NUM = 0;
    		now = 0;
    	}
    	
    	if(keyCode == KeyEvent.KEYCODE_BACK){
    		now = System.currentTimeMillis();
    		KEYCODE_BACK_NUM ++ ;
    		Log.i("AirplaneModeOpNewActivity", "onKeyDown,keyCode="+keyCode);
    		new AirplaneModeToast(getApplicationContext(), R.string.exitUI_showMsg).show();
    		return false;
    	}
    	
    	return false;
    }
    
}
